import Router from "@/router"
import {setLocalData,getLocalData,removeLocalData} from "@/common/utils"
import api from "@/api/api.js"
import {Message} from "element-ui"


export default {
	namespaced: true,
	state:{
		userInfo:getLocalData('userInfo'),
		authorityList: getLocalData('authList') || null,//权限列表
	},
	getters:{
	},
	actions:{
		async login({ commit,dispatch}, params){
			try{
				const data = await api.login(params);
				if(data){
					await commit("setUserInfo",data);
					await dispatch("getAuthority");
				}
			}catch(e){
				console.log(e)
			}
		},
		async logout({commit}){
			try{
				await commit("clearLocalData");
			}catch(e){
				console.log(e)
			}
		},
		async getAuthority({commit}){
			// try{
			// 	const data = await api.getAuthority();
				await commit("setAuthorityList",[]);
			// }catch(e){
			// 	console.log(e)
			// }
			
		}
		
	},
	mutations:{
		setUserInfo(state,userInfo){
			state.userInfo = userInfo;
			setLocalData("userInfo",userInfo);
		},
		clearLocalData(state){
			removeLocalData("userInfo");
			removeLocalData("authList");
			Message.success('退出成功！');
			setTimeout(() => {
			   location.reload();
			},500)
		},
		setAuthorityList(state,authList){
			state.authList = authList;
			setLocalData("authList",authList);
			Message.success('登录成功！');
			setTimeout(() => {
				location.reload();
			},500)
		}
	}
}